package sft.BancodeDados;

import sft.Logica.Insumos;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Dao{

    public static Connection conexao;
    public Dao() throws SQLException {
        this.conexao = new CriaConexao().getConnection();
    }
    
     /**
     * @param caso
     * adicionar
     * , editar
     * , deletar
     * @param l
     * atendente
     * , unidade
     * , convenio
     * , não conformidade
     * @throws SQLException
     */
    public void Operacao(String caso, Insumos l) throws SQLException{
        try (PreparedStatement stmt = conexao.prepareStatement(l.getSql())) {
            switch(caso){
                case "adicionar":
                    setAdd(stmt, l);
                    break;
                    
                case "editar":
                    setUpdate(stmt, l);
                    break;
                    
                case "deletar":
                    setDelete(stmt, l);
                    break;                
            }                
            stmt.execute();
            stmt.close();
            conexao.close();
        }
        
    }

    private void setAdd(PreparedStatement stmt, Insumos l) throws SQLException {
        try {
            stmt.setString(1, l.getNome());
            stmt.setString(2, l.getUser());
        } catch (Exception e) {
            stmt.setString(1, l.getNome());
        }
    }

    private void setUpdate(PreparedStatement stmt, Insumos l) throws SQLException {
        try {
            stmt.setString(1, l.getNome());
            stmt.setString(2, l.getUser());
            stmt.setInt(3, l.getId());
            
        } catch (Exception e) {
            stmt.setString(1, l.getNome());
            stmt.setInt(2, l.getId());
        }
    }

    private void setDelete(PreparedStatement stmt, Insumos l) throws SQLException {
        stmt.setInt(1, l.getId());
    }
}
